//
//  YCTotalEarningViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/24.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCTotalEarningViewController.h"
#define YCMonthEarnCellIdentify @"YCMonthEarnTableViewCell"

@interface YCTotalEarningViewController ()
{
    UILabel *m_labIncome;
    UILabel *m_labExpand;
    
    UITableView *m_tableView;
    
    NSMutableArray *m_arrayLists;
}
@end

@implementation YCTotalEarningViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:NO];
    [_naviVC setMainNaviVC];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"本年度";
    self.navigationItem.leftBarButtonItem=[self tbarBackButtonWhite];
    
    m_arrayLists=[NSMutableArray array];
    [self addMainView];
    [self loadData];
}
-(void)loadData
{
    StatisticsYearRequest *request = [StatisticsYearRequest new];
    [apiClient hideProgress];
    [apiClient disableAfterRequest];
    [apiClient doStatisticsYear:request success:^(ApiResponse *data, NSString *url) {
        StatisticsYearResponse *response=[[StatisticsYearResponse alloc]initWithCGResponse:data];
        [m_labIncome setText:response.income_total];
        [m_labExpand setText:response.outgo_total];
        m_arrayLists=[NSMutableArray arrayWithArray:response.list];
        [m_tableView reloadData];
    }failure:^(ApiResponse *data, NSString *url){
    }];
}

-(void)addMainView
{
    UIView *backView=[[UIView alloc]init];
    [backView setBackgroundColor:MAIN_COLOR];
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(self.view).offset(kNAV_HEIGHT);
        make.height.mas_equalTo(100);
    }];
    
    CGFloat X=kMainScreen_Width/2;
    [backView.layer addSublayer:getLine(X, X, 30, 70, WHITE_COLOR)];
    
    
    UILabel *label1=[[UILabel alloc]init];
    [label1 setTextColor:WHITE_COLOR];
    [label1 setFont:[UIFont LightFontOfSize:15]];
    [label1 setTextAlignment:NSTextAlignmentCenter];
    [backView addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView).offset(0);
        make.width.mas_equalTo(X);
        make.top.equalTo(backView).offset(20);
        make.height.mas_equalTo(15);
    }];
    [label1 setText:@"累计收入"];

    
    UILabel *label2=[[UILabel alloc]init];
    [label2 setTextColor:WHITE_COLOR];
    [label2 setFont:[UIFont LightFontOfSize:15]];
    [label2 setTextAlignment:NSTextAlignmentCenter];
    [backView addSubview:label2];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backView).offset(0);
        make.width.mas_equalTo(X);
        make.top.equalTo(backView).offset(20);
        make.height.mas_equalTo(15);
    }];
    [label2 setText:@"累计支出"];
    
    
    m_labIncome=[[UILabel alloc]init];
    [m_labIncome setTextColor:WHITE_COLOR];
    [m_labIncome setFont:[UIFont FontOfSize:22]];
    [m_labIncome setTextAlignment:NSTextAlignmentCenter];
    [backView addSubview:m_labIncome];
    [m_labIncome mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backView).offset(0);
        make.width.mas_equalTo(X);
        make.top.equalTo(label1.bottom).offset(kDistance);
        make.height.mas_equalTo(25);
    }];
    
    m_labExpand=[[UILabel alloc]init];
    [m_labExpand setTextColor:WHITE_COLOR];
    [m_labExpand setFont:[UIFont FontOfSize:22]];
    [m_labExpand setTextAlignment:NSTextAlignmentCenter];
    [backView addSubview:m_labExpand];
    [m_labExpand mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backView).offset(0);
        make.width.mas_equalTo(X);
        make.top.equalTo(label2.bottom).offset(kDistance);
        make.height.mas_equalTo(25);
    }];
    
    
    m_tableView=[[UITableView alloc]init];
    m_tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    m_tableView.delegate=self;
    m_tableView.dataSource=self;
    [self.view addSubview:m_tableView];
    [m_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(0);
        make.right.equalTo(self.view).offset(0);
        make.top.equalTo(backView.bottom).offset(0);
        make.bottom.equalTo(self.view).offset(0);
    }];
    [m_tableView registerNib:[UINib nibWithNibName:YCMonthEarnCellIdentify bundle:nil] forCellReuseIdentifier:YCMonthEarnCellIdentify];
    
    UIView *header=[[UIView alloc]initWithFrame:CGRectMake(0, 0, W(m_tableView), kSmallDistance)];
    m_tableView.tableHeaderView=header;
}
#pragma mark tableview datasource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return m_arrayLists.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
#pragma mark tableview delegate
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    StatisticsYearTable *statis=m_arrayLists[indexPath.section];
    if (statis.selected) {
        return 50+statis.item.count*40;
    }
    return 50;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (nil == cell){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    YCMonthEarnTableViewCell *_cell = (YCMonthEarnTableViewCell *)[tableView dequeueReusableCellWithIdentifier:YCMonthEarnCellIdentify forIndexPath:indexPath];
    
    StatisticsYearTable *statis=m_arrayLists[indexPath.section];
    [_cell bindWith:statis];
    _cell.headerBlock=^(){
        statis.selected=!statis.selected;
        [m_tableView reloadData];
    };
    _cell.detailBlock=^(StatisticsItemTable *item){
        YCEarningListViewController *listVC=[[YCEarningListViewController alloc]init];
        listVC.month=statis.month;
        listVC.deal_type=item.deal_type;
        [self showNavigationView:listVC];
    };
    
    cell=_cell;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end








